<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title></title>
    <script src="../../static/plugins/js/uni-webview.js"></script>
    <script src="../../static/plugins/js/axios.min.js"></script>
  </head>
  <body style="background: #000; width: 100%; height: 100%"></body>
  <script>
    let wanLen = 0;
    let wandata = [];
    window.download = function (data) {
      let list = data.list;
      let searchForm = data.form;
      let url = data.url;
      console.log(JSON.stringify(list));
      wanLen = list.length;
      console.log("长度:" + wanLen);
      for (let i = 0; i < wanLen; i++) {
        let item = list[i];
        getData({
          url: url,
          params: {
            tableNameC: item.tableNameC || "",
            tableNameE: item.tableNameE || "",
            stratTimeName: item.stratTimeName || "",
            endTimeName: item.endTimeName || "",
            startTime: searchForm.startTime,
            endTime: searchForm.endTime,
          },
        });
      }
    };
    function getData(data) {
      axios({
        url: data.url,
        method: "post",
        data: data.params,
        responseType: "blob",
      }).then((res) => {
        console.log(JSON.stringify(res));
        wandata.push({
          ...data.params,
          result: res.data,
        });
        if (wandata.length == wanLen) {
          console.log(JSON.stringify(wandata));
          uni.postMessage({
            data: JSON.stringify(wandata),
          });
        }
        // console.log(JSON.stringify(res));
      });
    }
    // 将arraybuffer数据转为base64
    function arrayBufferToBase64(buffer) {
      var binary = "";
      var bytes = new Uint8Array(buffer);
      var len = bytes.byteLength;
      for (var i = 0; i < len; i++) {
        binary += String.fromCharCode(bytes[i]);
      }
      return window.btoa(binary);
    }
    var prefix = "";
    // 将base64的数据转为blob数据
    function base64toBlob(base64, type = "application/octet-stream") {
      var dataArr = base64.split("base64,");
      prefix = dataArr[0] + "base64,";
      var data = dataArr[1];
      const bstr = atob(data);
      let n = bstr.length;
      const u8arr = new Uint8Array(n);
      while (n--) {
        u8arr[n] = bstr.charCodeAt(n);
      }
      return new Blob([u8arr], { type });
    }
  </script>
</html>
